<?php

namespace App\Controller;

use App\Service\FileService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;

#[AutoController(prefix: "file")]
class FileController extends AbstractController
{

    #[Inject]
    protected FileService $fileService;

    public function upload()
    {
        if (!$this->request->hasFile('file')){
            return $this->fail('请上传文件');
        }
        [$r, $d] = $this->fileService->uploadFileOne($this->request->file('file'));
        if ($r){
            return $this->success($d,'上传成功');
        }
        return $this->fail($d);
    }

    public function writeFile()
    {
        $content = $this->request->input('content','');
        if (!$content){
            return $this->fail('内容不能为空');
        }
        $fileName = $this->request->input('file_name','');
        if (!$fileName){
            return $this->fail('内容不能为空');
        }
        [$r, $d] = $this->fileService->writeFile($content,$fileName);
        if ($r){
            return $this->success($d,'上传成功');
        }
        return $this->fail($d);
    }
}